package com.chiga.interceptor;


import org.apache.commons.beanutils.NestedNullException;
import org.apache.commons.beanutils.PropertyUtils;


import com.chiga.utility.date.DateUtil;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.PreResultListener;

public class ParseDateInterceptor extends AbstractInterceptor implements PreResultListener{
	
	private static final long serialVersionUID = -2498171364816538294L;
	public String parse;

	/*
	private void setReservedSerialStr(Object action) {
		Object o = null;
		try {
			o = PropertyUtils.getProperty(action, "reservedContainers");
			PropertyUtils.setProperty(action, "reservedSerialStr", ObjectSerializer.getObjectAsString(o));
		} catch (IllegalAccessException e1) {
			
			e1.printStackTrace();
		} catch (InvocationTargetException e1) {
			
			e1.printStackTrace();
		} catch (NoSuchMethodException e1) {
			
			e1.printStackTrace();
		} catch (Exception e) {
			
			e.printStackTrace();
		}
	}
	*/
	public void beforeResult(ActionInvocation ai, String resultCode) {
		Object action = ai.getAction();
		//setReservedSerialStr(action);
		String[] properties = getParseArray();
		try {
			for (String property : properties){
				if (checkProperty(action, property)) {
					setUIDate(action, property, getActionValue(action, property));
				}
			}
		} catch (Exception e) {
			
			e.printStackTrace();
		}
	}

	private boolean checkProperty(Object action, String property) throws Exception{
		boolean result = false;
		String[] properties = property.split("\\.");
		int last = property.trim().lastIndexOf(properties[properties.length-1]);
		if (last == 0) last = property.length()+1;	
		String checkProperty = property.substring(0, last - 1);
		try	{
			if (PropertyUtils.getNestedProperty(action, checkProperty) == null)	{
				result = false;
			}else{
				result = true;
			}
		}catch(NullPointerException npe)	{
			result = false;
		}catch (NestedNullException nne)	{
			result = false;
		}catch (NoSuchMethodException nme)	{
			result = false;
		}
		return result;
	}

	private String getActionValue(Object action, String property) throws Exception	{		
		String result = null;
		if (checkProperty(action, property)) {
			result = (String)PropertyUtils.getNestedProperty(action, property);
		}
		return result;
	}

	/**
	 * 將日期轉換為UI上的樣式
	 * @param action
	 * @param property
	 * @param value
	 * @throws Exception
	 */
	private void setUIDate(Object action, String property, String value) throws Exception	{
		if (value == null || value.equals("")) return;
		if (value != null && value.indexOf("/") > 0) return;
		
		String newDate;
		if (property.endsWith("Dtm")) {
			newDate = DateUtil.format(DateUtil.parse(value), DateUtil.DISPLAY_DATETIME_FORMAT);
		} else {
			newDate = DateUtil.format(DateUtil.parse(value), DateUtil.DISPLAY_DATE_FORMAT);
		}
		PropertyUtils.setNestedProperty(action, property, newDate);
	}

	/**
	 * 將UI上的日期轉回SERVER上的樣式
	 * @param action
	 * @param property
	 * @param value
	 * @throws Exception
	 */
	private void setParseDate(Object action, String property, String value) throws Exception {
		if (value == null) return;
		if (value.indexOf("/") < 0)	{
			PropertyUtils.setNestedProperty(action, property, value);
		}else{
			String newDate;
			if (property.endsWith("dtm")) {
				newDate = DateUtil.format(DateUtil.parse(value), DateUtil.DISPLAY_DATETIME_FORMAT);
			} else {
				newDate = DateUtil.format(DateUtil.parse(value), DateUtil.DATETIME_FORMAT);
			}
			PropertyUtils.setNestedProperty(action, property, newDate);
		}
	}

	@Override
	public String intercept(ActionInvocation ai) throws Exception {
		ai.addPreResultListener(this);
		Object action = ai.getAction();
		String[] properties = getParseArray();
		for (String property : properties) {
			if (checkProperty(action, property)) {
				setParseDate(action, property, getActionValue(action, property));
			}
		}
		return ai.invoke();
	}
	
	private String[] getParseArray() {
		if (parse == null) return new String[0];
		String[] parses = parse.split(",");
		String[] result= new String[parses.length];
		for (int i = 0; i < parses.length; i++)	{
			result[i] = parses[i].trim();
		}
		return result;
	}
}
